package com.lanju.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lanju.common.SpringUtils;
import com.lanju.entity.House;
import com.lanju.entity.HousePrice;
import com.lanju.entity.User;
import com.lanju.service.HouseService;
import com.lanju.service.JedisService;

@Controller
@RequestMapping("/house")
public class HouseController {

	@Autowired
	HouseService houseService;
	
	 @Resource
	 JedisService jedisService;

	/**
	 * 租房信息列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryHouseList")
	@ResponseBody
	public Map<String, Object> queryHouseList(HttpServletRequest request) {
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return houseService.queryHouseList(param);
	}

	/**
	 * 房屋详情
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryHomeDetailByHouseId")
	@ResponseBody
	public Map<String, Object> queryHomeDetail(HttpServletRequest request,HttpSession session) {
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		 User user =(User) session.getAttribute("userModel");
		 if(user !=null){
			 param.put("userId", user.getUserId()); 
		 }
		return houseService.queryHomeDetailByHouseId(param);

	}

	/**
	 * 获取房子所有筛选标签（数据取自码表lj_house_flag）
	 * 
	 * @return
	 */
	@RequestMapping("/queryAllFlag")
	@ResponseBody
	public Map<String, Object> queryAllFlag(HttpServletRequest request) {
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return houseService.queryAllFlag(param);
	}

	@RequestMapping("/getById")
	@ResponseBody
	public Map<String, Object> getById(HttpServletRequest request) {
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return houseService.getById(param);
	}

	@RequestMapping("/queryMyHouseList")
	@ResponseBody
	public Map<String, Object> queryMyHouseList(HttpServletRequest request,HttpSession session) {
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		 User user =(User) session.getAttribute("userModel");
		param.put("userId", user.getUserId());
		// param.put("userId", "92e95216-45b1-45b3-a086-5b3ba2257958");
		return houseService.queryMyHouseList(param);
	}

	@RequestMapping("/addHousePrice")
	@ResponseBody
	public Map<String, Object> addHousePrice(HttpServletRequest request,HttpSession session) {
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		 User user =(User) session.getAttribute("userModel");
		try {
			// HousePrice hp=(HousePrice) ObjAndMapUtil.mapToObject(param,
			// HousePrice.class);
			HousePrice hp = new HousePrice();
			hp.setCity(param.get("city") == null ? "" : param.get("city")
					.toString());
			hp.setCoummity_name(param.get("community_name") == null ? "" : param
					.get("community_name").toString());
			hp.setDegree(param.get("degree") == null ? "" : param.get("degree")
					.toString());
			hp.setTowards(param.get("towards") == null ? "" : param
					.get("towards").toString());
			hp.setHouse_count(param.get("house_count") == null ? "" : param
					.get("house_count").toString());
			hp.setHouse_num(param.get("house_num") == null ? "" : param.get(
					"house_num").toString());
			hp.setHouse_size(param.get("house_size") == null ? 0 : Integer
					.valueOf(param.get("house_size").toString()));
			hp.setLeaseLength(param.get("leaseLength") == null ? 0 : Integer
					.valueOf(param.get("leaseLength").toString()));
			hp.setCreator(user.getUserId());
			hp.setCreate_time(new Date());
			return houseService.addHousePrice(hp);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 获取用户所有的房子
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getAllHouseList")
	@ResponseBody
	public Map<String,Object> getAllHouseList(HttpServletRequest request){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return houseService.getAllHouseList(param);
	}
	

	public ModelAndView redirectHome(User user){
		 System.out.println(user.getNickName());
		 ModelAndView mav = new ModelAndView("forward:/jsp/home");
		 mav.addObject(user);
		 return mav;
	}
	
	@RequestMapping("/getUserList")
	@ResponseBody
	public Map<String,Object> getUserList(HttpServletRequest request){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		Map<String,Object> houseList=houseService.getAllHouseList(param);
		if(houseList !=null){
			Set<House> house =	(Set<House>) houseList.get("list");
			Iterator<House> iter=house.iterator();
			while(iter.hasNext()){
				House h=iter.next();
			}
		}
		return null;
	}
	@RequestMapping("/loadZuKeInfo")
	@ResponseBody
	public Map<String,Object> loadZuKeInfo(HttpServletRequest request){
		Map<String,Object> result=new HashMap<String, Object>();
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		result.put("info", houseService.getHouseZuKeInfo(param));
		return result;
	}
}
